<?php

namespace app\supplier\controller;

use app\company\model\StoreSupplier;
use app\supplier\validate\StoreValidate;
use app\service\traits\Crud;
use app\supplier\model\StoreUserAccount;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;
use app\supplier\model\Store as StoreModel;

class Store extends Base
{
    private StoreSupplier $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new StoreSupplier();
        $this->validateClass = StoreValidate::class;
        $this->field         = 'id,store_id,supplier_id';
        $this->withSearch    = 'store_id';
    }


    /**
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list(): Response
    {
        $param = request()->param();
        validate($this->validateClass)->scene('list')->check($param);
        $sql   = $this->model->supplierId($this->supplier_id)->withSearch($this->withSearch, $param)->field($this->field);
        $count = $sql->count();
        if (Request::param('is_export', 0) != 1) $sql = $sql->page($param['page'], $param['limit']);
        $list = $sql->order($this->orderSort)->select()->each(function ($item) {
            $item->store      = $item->store()->field('id,name,address,is_on_credit,create_time')->find()->append(['is_on_credit_text']);
            $item->store_user = $item->store->storeUsers()->field('real_name,mobile')->find();
        });
        return $this->success('获取成功', compact('count', 'list'));
    }

    /**
     * 详情
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail(): Response
    {
        $store_id = request()->param('store_id',0);
        $field = "id,name,address,is_on_credit,long,lat,credit_days";
        $info  = StoreModel::where('id', $store_id)->field($field)->find();
        if (!$info) return $this->error('数据不存在');
        $info->append(['is_on_credit_text']);
        $info->store_user      = $info->storeUsers()->field('id,real_name,mobile')->find();
        $info->store_user_bank = $info->storeUsers()->find()->storeUserBanks()->field("bank_type,bank_no,user_name,bank_branch")->find();
        if (!$info->store_user_bank) {
            $arr['bank_type']      = 1;
            $arr['bank_no']        = '';
            $arr['user_name']      = '';
            $arr['bank_branch']    = '';
            $info->store_user_bank = $arr;
        }
        $info->money = StoreUserAccount::where('store_user_id', $info->store_user->id)->value('money');//负责人余额
        return $this->success('获取成功', compact('info'));
    }

    /**
     * 筛选框
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function search(): Response
    {
        $param = request()->param();
        $sql   = new StoreSupplier();
        $sql = $sql->alias('sup')->join(['dt_store'=>'store'],'store.id=sup.store_id')->where('sup.supplier_id',$this->supplier_id);
        if($name = request()->param('name','')){
            $sql = $sql->where('store.name','like','%'.$name.'%');
        }
        $count = $sql->count();
        if (request()->param('page', 0)) {
            $sql   = $sql->page($param['page'], $param['limit']);
        }
        $field = 'store.id,store.name';
        $list = $sql->field($field)->order($this->orderSort)->select();
        return $this->success('获取成功', compact('list', 'count'));
    }
}